<?php
class BaseAction extends Action {
	public function _initialize() {
		
		//临时验证是否使用该模块
		if(true){
			$tempverify = $_REQUEST['tempverify'];
			if(empty($tempverify))$tempverify=$_SESSION['plugininfo']['tempverify'];
			if($tempverify!='yes'){
				$this->error('暂时未开放该功能');
			}
			$_SESSION['plugininfo']['tempverify']=$tempverify;
		}
		//检查是否已登录
		$this->gotoLogin();
		
		//判断登录用户角色
		if(!($this->whoCanOperate('agent'))){
			$this->assign('jumpUrl',U('agent://Public/login'));
			$this->error('联盟会员登录，才可以进行该操作！');
		}
		
		//用户中心，基本功能对应数据 
		$userNavDict['Account'] = array('index'=>'欢迎 '.$this->user['uname']
										,'sendList'=>'分成统计'
										,'rebatepay'=>'支付记录'
										,'profile'=>'会员资料'
										,'security'=>'安全设置'
										,'pluginsetup'=>'插件代码'
										,'dkxd'=>'代客送礼'
							);
		$this->assign('userNavDict', $userNavDict);
		$status = $this->isAgentActive();
		$this->assign('agentstatus',$status);
	}
	
	//检查Email是否可用
	public function isEmailAvailable($email = null) {
		$return_type = empty($email) ? 'ajax' 		   : 'return';
		$email		 = empty($email) ? $_POST['email'] : $email;
		
		$res = M('user')->where('`email`="'.$email.'"')->find();
		
		if ( !$res ) {
			if ($return_type === 'ajax') echo 'success';
			else return true;
		}else {
			if ($return_type === 'ajax') echo '邮箱已注册';
			else return false;
		}
	}
	
	/**
	 * 判断是否代理申请通过
	 * 返回标准json数据格式
	 */
	protected function isAgentActive(){
		$agent = M('Agent')->where('user_id='.$this->mid)->find();
		//dump($agent);
		if(empty($agent)){
			return array('status'=>0,'msg'=>'未申请网盟1');
		}
		if($agent['enable']=='N'){
			return array('status'=>-1,'msg'=>'网盟资格申请中2');
		}
		return array('status'=>1,'msg'=>'网盟资格有效');
	}
}